package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.microservice.open.service.gateway.manager.web.api.IRouteServiceUrlServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteServiceUrlAddAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteServiceUrlUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RouteServiceUrlAResp;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.RouteServiceUrl;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.RouteServiceUrlEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.RouteServiceUrlService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RouteServiceUrlServiceRemoteImpl implements IRouteServiceUrlServiceRemote {
    @Autowired
    private RouteServiceUrlService routeServiceUrlService;

    @Override
    public ResponseData<RouteServiceUrlAResp> detail(@PathVariable("id") Integer id) {
        ResponseData<RouteServiceUrlAResp> responseData = new ResponseData();

        RouteServiceUrlEnhanced routeServiceUrlEnhanced = routeServiceUrlService.getEnhanced(id, null);

        responseData.setData(CommonConvent.convent2RouteServiceUrlAResp(routeServiceUrlEnhanced));

        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<RouteServiceUrlUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();

        RouteServiceUrlUpdateAReq routeServiceUrlUpdateAReq = requestData.getRequest();

        routeServiceUrlService.updateRouteServiceUrl(routeServiceUrlUpdateAReq.getId(), routeServiceUrlUpdateAReq.getUrl(), routeServiceUrlUpdateAReq.getStatus(), routeServiceUrlUpdateAReq.getComment(), null);

        return responseData;
    }

    @Override
    public ResponseData<RouteServiceUrlAResp> add(@RequestBody RequestData<RouteServiceUrlAddAReq> requestData) {
        ResponseData<RouteServiceUrlAResp> responseData = new ResponseData<>();
        RouteServiceUrlAddAReq routeServiceUrlAddAReq = requestData.getRequest();

        RouteServiceUrl routeServiceUrl = new RouteServiceUrl();
        BeanUtils.copyProperties(routeServiceUrlAddAReq, routeServiceUrl);

        routeServiceUrl = routeServiceUrlService.saveRouteServiceUrl(routeServiceUrl, null);

        responseData.setData(CommonConvent.convent2RouteServiceUrlAResp(routeServiceUrlService.convent2Enhanced(routeServiceUrl)));

        return responseData;
    }

    @Override
    public ResponseData delete(@PathVariable("id") Integer id) {
        ResponseData responseData = new ResponseData();

        routeServiceUrlService.deleteById(id, null);

        return responseData;
    }
}
